///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include	<LightPoint.h>
#include	<MGLib.h>

//-----------------------------------------------------------------------------

PointLight::PointLight()
{
	m_obj = NULL;
	m_range = 0.0f;
}

//-----------------------------------------------------------------------------

PointLight::~PointLight()
{
	SAFE_DEL( m_obj );
}

//-----------------------------------------------------------------------------

void	PointLight::Init()
{
	MeshData *meshData = MeshManager::GetInstance()->GetMesh( "sfera.obj" );
	m_range = 1.0f;

	Material* mat = MGL_NEW Material();
	m_shader = mat->m_shaders[0] = ShaderManager::GetInstance()->GetShader( "point_light-glsl" );
	mat->m_shaders[1] = ShaderManager::GetInstance()->GetShader( "color-mrt-glsl" );

	mat->m_diffuse[0] = 1.0f;

	Mesh* mesh = MGL_NEW Mesh( "point_light" );
	mesh->SetMeshData( meshData );
	mesh->SetMaterial( mat );

	m_obj = MGL_NEW Object();
	m_obj->AddMesh( mesh );
}

//-----------------------------------------------------------------------------

void		PointLight::SetPosition( GLfloat x, GLfloat y, GLfloat z )
{
	if( m_obj )
	{
		m_obj->setPosition( x, y, z );
	}
}

//-----------------------------------------------------------------------------

Vec3f	PointLight::GetPosition()
{
	if( m_obj )
	{
		return m_obj->getPosition();
	}
	return Vec3f();
}

//-----------------------------------------------------------------------------

void		PointLight::SetColor( GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha )
{
	if( m_obj )
	{
		Material* mat = m_obj->GetMesh(0)->GetMaterial();
		if( mat )
		{
			mat->m_diffuse[0] = red;
			mat->m_diffuse[1] = green;
			mat->m_diffuse[2] = blue;
			mat->m_diffuse[3] = alpha;
			mat->SetParameter4f( "lightColor", mat->m_diffuse );
		}
	}
}

//-----------------------------------------------------------------------------

void		PointLight::SetRange( GLfloat range )
{
	m_range = range;
	if( m_obj )
	{
		m_obj->setScale( range, range, range );
		m_obj->GetMesh(0)->GetMaterial()->SetParameter1f( "lightRange", m_range );
	}
}

//-----------------------------------------------------------------------------

GLfloat	PointLight::GetRange()
{
	return m_range;
}

//-----------------------------------------------------------------------------

void PointLight::Update()
{
}

//-----------------------------------------------------------------------------

void PointLight::Render()
{
}

//-----------------------------------------------------------------------------

void PointLight::RenderDebug(GPUProgram* shader)
{
	glPushMatrix();
	Vec3f pos;
	if( m_obj )
	{
		pos = m_obj->getPosition();
	}
	glTranslatef( pos.x, pos.y, pos.z );
	glColor3f( m_color[0], m_color[1], m_color[2] );
	//glutSolidSphere( 0.1f /*m_range / 20.f*/,20,20);
	//glutSolidSphere( m_range ,20,20);
	m_obj->GetMesh(0)->GetMeshData()->Draw();
	glPopMatrix();
}
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
